/**
 * 
 */
package de.cbf.cam.dsa.model.entities;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import de.cbf.cam.dsa.model.persistence.Header;

/**
 * @author Nebelritter
 *
 */
@Entity
public class FullArmor  extends Header{
	
	/**
	 * @uml.property  name="armorParts"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.ArmorPart"
	 */
	private List<ArmorPart> armorParts = new ArrayList<ArmorPart>();
	
	private final static String PROPERTYNAME = "FullArmor:";
	
	/**
	 * @uml.property  name="propertyChangeSupport"
	 */
	public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);	
	
	/**
	 * @uml.property  name="armorPartPropertyChange"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.model.entities.FullArmor$FullArmorPropertyAdvance"
	 */
	public FullArmorPropertyAdvance armorPartPropertyChange = new FullArmorPropertyAdvance();
//========================
//		constructors
//========================
	public FullArmor() {
		super();		
	}
//========================
//	   public methods
//========================




//========================
//    standard methods
//========================

//========================
// 	   private methods
//========================

//========================
// 	SHORT property access
//========================
	public boolean addArmorPart(ArmorPart part){
		part.propertyChangeSupport.addPropertyChangeListener(armorPartPropertyChange);
		boolean success = armorParts.add(part);
		propertyChangeSupport.firePropertyChange(PROPERTYNAME,null,armorParts);
		return success;
	}
	
	public boolean removeArmorPart(ArmorPart part){
		part.propertyChangeSupport.removePropertyChangeListener(armorPartPropertyChange);
		boolean success = armorParts.remove(part);
		propertyChangeSupport.firePropertyChange(PROPERTYNAME,null,armorParts);
		return success;
	}
//========================
//		getters
//========================
	/**
	 * @return the name
	 */
	@Transient
	public String getName() {
		StringBuffer buffer = new StringBuffer();
		for(int i=0;i<armorParts.size();i++){
			buffer.append(armorParts.get(i).getName());
			if(i!=armorParts.size()-1){
				buffer.append(",");
			}
		}
		return buffer.toString();
	}
	/**
	 * @return the rs
	 */
	@Transient
	public int getRs() {
		int result=0;		
		for(int i=0;i<armorParts.size();i++){
			result+=armorParts.get(i).getRs();
		}
		return result;
	}
	
	/**
	 * @return the rs
	 */
	@Transient
	public int getRs(int zone) {
		int result=0;
		for(int i=0;i<armorParts.size();i++){
			result+=armorParts.get(i).getRs(zone);		
		}
		return result;
	}
	/**
	 * @return the be
	 */
	@Transient
	public int getBe() {
		int result=0;		
		for(int i=0;i<armorParts.size();i++){
			result+=armorParts.get(i).getBe();
		}
		return result;
	}
	/**
	 * @return the structure
	 */
	@Transient
	public int getStructure() {
		int result=0;		
		for(int i=0;i<armorParts.size();i++){
			result+=armorParts.get(i).getStructure();
		}
		return result;
	}
	/**
	 * @return the structure
	 */
	@Transient
	public int getStructure(int zone) {
		int result=0;
		for(int i=0;i<armorParts.size();i++){
			result+=armorParts.get(i).getStructure(zone);		
		}
		return result;
	}
	/**
	 * @return the armorParts
	 */
	@OneToMany(cascade=CascadeType.ALL)
	public List<ArmorPart> getArmorParts() {
		return armorParts;
	}
	/**
	 * @param armorParts the armorParts to set
	 */
	protected void setArmorParts(List<ArmorPart> armorParts) {
		this.armorParts = armorParts;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME,null,armorParts);
	}	
//========================
//		setters
//========================
	

//========================
//		listeners
//========================
	private class FullArmorPropertyAdvance implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+evt.getPropertyName(), null,FullArmor.this);			
		}		
	}
	
	@Override
	@Id
	public String getGuid() {
		return guid;
	}
	@Override
	protected void setGuid(String guid) {
		this.guid = guid;		
	}
	
}
